package org.ourgrid.broker.commands.executors;

import org.ourgrid.broker.BrokerConstants;
import org.ourgrid.broker.commands.SchedulerData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;
import br.edu.ufcg.lsd.commune.identification.ContainerID;
import br.edu.ufcg.lsd.commune.processor.filetransfer.IncomingTransferHandle;
import br.edu.ufcg.lsd.commune.processor.filetransfer.TransferReceiver;

public class AcceptTransferExecutor extends SchedulerCommandExecutor {
	
	private long id;
	private String localFileName;
	private String destinationFileName;
	private String description;
	private String senderContainerID;
	private long fileSize;

	/**
	 * <ACCEPT_TRANSFER id='long' localFileName='String' destinationFileName='String' description='String' 
	 * senderContainerID='String' fileSize='long'/>
	 */
	public void execute(SchedulerData data, ServiceManager manager) {
		
		fillData(data.getXml());
		
		IncomingTransferHandle tHandle = new IncomingTransferHandle(id, localFileName, fileSize, 
				destinationFileName, description, ContainerID.parse(senderContainerID));
		
		TransferReceiver receiver = (TransferReceiver) manager.getObjectDeployment(
				BrokerConstants.WORKER_CLIENT).getObject();
		
		manager.acceptTransfer(tHandle, receiver);
	}
	
	private void fillData(String xml) {
		
		Document logDocFile = super.getXMl(xml);
		
		Element acceptTransfer = logDocFile.getDocumentElement();
		
		id = Long.valueOf(acceptTransfer.getAttribute("id"));
		localFileName = acceptTransfer.getAttribute("localFileName");
		destinationFileName = acceptTransfer.getAttribute("destinationFileName");
		description = acceptTransfer.getAttribute("description");
		senderContainerID = acceptTransfer.getAttribute("senderContainerID");
		fileSize = Long.valueOf(acceptTransfer.getAttribute("fileSize"));
	}
}
